import React, {Component} from 'react';
import {Modal, Input, Radio, Form} from 'antd';
import GameAction from '../../action/GameAction';
import GameInfoForm from './GameInfoForm'
import BaseUrl from "../../common/BaseUrl";

let gameAction = new GameAction();
const FormItem = Form.Item;

class GameInfoAddModal extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: props.visible,
            maskClosable: false
        };
        this.handleOk = this.handleOk.bind(this);
        this.handleCancel = this.handleCancel.bind(this);
        this.afterClose = this.afterClose.bind(this);
    }

    handleOk(type) {
        if (type) {
            this.saveGameInfo();
        } else {
            this.updateGameInfo();
        }
    }

    saveGameInfo() {
        //空判断
        let url = BaseUrl + 'react/gameInfo/addGameInfo';
        let formData = new FormData();
        let gameName = document.getElementById("gameName").value;
        let gameType = document.getElementById("gameType").value;
        formData.append("gameName", gameName);
        formData.append("gameType", gameType);
        fetch(url, {
            method: 'post',
            body: formData,
            headers: {
                'Accept': 'application/json'
            },
            credential: 'same-origin'
        }).then(() => {
            gameAction.changeModalVisible(this.state.visible);
            this.props.refresh();
        });
    }

    updateGameInfo() {
        let gameId = this.props.gameId;
        let gameName = document.getElementById("gameName").value;
        let gameType = document.getElementById("gameType").value;
        let formData = new FormData();
        formData.append("gameId", gameId);
        formData.append("gameName", gameName);
        formData.append("gameType", gameType);
        let url = BaseUrl + 'react/gameInfo/updateGameInfo';
        fetch(url, {
            method: 'post',
            body: formData,
            headers: {
                'Accept': 'Application/json'
            },
            credential: 'same-origin'
        }).then((res) => {
            gameAction.changeModalVisible(this.state.visible);
            this.props.refresh();
        }).catch((err) => {
            console.log(err);
        })
    }



    afterClose() {
        document.getElementById("gameName").value = "";
        document.getElementById("gameType").value = "";
    }

    handleCancel() {
        gameAction.changeModalVisible(this.state.visible);
    }

    render() {
        let type = this.props.type;
        return (
            <div>
                <Modal title={this.props.title} visible={this.props.visible}
                       onOk={() => this.handleOk(type)} onCancel={this.handleCancel} maskClosable={this.state.maskClosable} afterClose={this.afterClose}>
                    <GameInfoForm gameName={this.props.gameName} gameType={this.props.gameType}/>
                </Modal>
            </div>
        );
    }
}

export default GameInfoAddModal